package com.aduerr.v1shouyin.mvp.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.aduerr.v1shouyin.R;
import com.aduerr.v1shouyin.entity.*;
import com.aduerr.v1shouyin.http.APIRetrofit;
import com.aduerr.v1shouyin.http.RXRequest;
import com.aduerr.v1shouyin.util.ToastUtils;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.internal.observers.BlockingBaseObserver;
import io.reactivex.schedulers.Schedulers;

import java.util.HashMap;


/**
 * @author HI
 * @email dev1024@foxmail.com
 * @time 2018/10/23 14:42
 * @change
 * @chang time
 * @class describe
 */

public class KaQuanDetailNewActivity extends AppCompatActivity {


    public static final String TAG = "KaQuanNewDetailActivity";

    private static int defalutBackIconResID;

    static {
        defalutBackIconResID = R.drawable.img_back_new;
    }

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tv_toolbar_title)
    TextView toolbarTitle;
    //卡券码
    @BindView(R.id.card_num)
    TextView tvCardNum;
    //wx
    @BindView(R.id.ll_wx)
    LinearLayout llWx;
    @BindView(R.id.card_name)
    TextView tvWxCardName;
    @BindView(R.id.tv_type)
    TextView tvWxType;
    @BindView(R.id.card_time)
    TextView tvWxCardTime;
    //
    @BindView(R.id.ll_wxzhekou)
    LinearLayout llWXZheKou;
    @BindView(R.id.zhekou_order)
    TextView tvZheKouOrder;
    @BindView(R.id.ll_wxdaijin)
    LinearLayout llWXDaiJin;
    @BindView(R.id.daijin_order)
    TextView tvDaiJinOrder;
    @BindView(R.id.daijin_order_detail)
    TextView tvDaiJinOrderDetail;
    //团购券 门票
    @BindView(R.id.ll_tuangou)
    LinearLayout llTuanGou;
    @BindView(R.id.tuangou_name)
    TextView tvTuanGouName;
    @BindView(R.id.tv_tuangou_type)
    TextView tvTuanGouType;
    @BindView(R.id.order_money)
    TextView tvTuanGouMoney;
    @BindView(R.id.order_state)
    TextView tvTuanGouState;
    @BindView(R.id.order_time)
    TextView tvTuanGouOderTime;
    //酒店
    @BindView(R.id.ll_jiudian)
    LinearLayout llJiuDian;
    @BindView(R.id.jiudian_name)
    TextView tvJiuDianName;
    @BindView(R.id.jiudian_money)
    TextView tvJiuDianMoney;
    @BindView(R.id.jiudian_state)
    TextView tvJiuDianState;
    @BindView(R.id.order_jiudian_time)
    TextView tvJiudianOrderTime;
    @BindView(R.id.jiudian_contract)
    TextView tvJiudianContract;
    //礼品
    @BindView(R.id.ll_lipin)
    LinearLayout llLiPin;
    @BindView(R.id.order_name_lipin)
    TextView tvLiPinName;
    @BindView(R.id.lipin_name)
    TextView tvLiPinUserName;
    @BindView(R.id.lipin_time)
    TextView tvLiPinTime;
    @BindView(R.id.lipin_jifen_state)
    TextView tvLiPinJifen;
    @BindView(R.id.order_type_linpin)
    TextView tvOrderTypeLiPin;
    //口碑
    @BindView(R.id.ll_koubei)
    LinearLayout llKouBei;
    @BindView(R.id.order_name_koubei)
    TextView tvKouBeiName;
    @BindView(R.id.tv_koubei_realmoney)
    TextView tvKouBeiRealMoney;
    @BindView(R.id.koubei_nowmoney)
    TextView tvKouBeiNowMoney;
    @BindView(R.id.koubei_shishou)
    TextView tvKouBeiShiShou;
    @BindView(R.id.order_type_koubei)
    TextView tvKouBeiType;
    @BindView(R.id.koubei_time)
    TextView tvKouBeiTime;
    @BindView(R.id.koubei_order)
    TextView tvKouBeiOrder;
    @BindView(R.id.ll_koubei_about)
    LinearLayout llKouBeiAbout;
    //关于
    @BindView(R.id.rl_about)
    RelativeLayout rlAbout;
    @BindView(R.id.about_content)
    TextView tvAboutContent;
    //核销
    @BindView(R.id.btn_hexiao)
    Button btnHeXiao;
    @BindView(R.id.rl_time_card)
    RelativeLayout mRlTimeCard;
    @BindView(R.id.tv_time_card)
    TextView mTvTimeCard;


    String type;
    //21  34  8
    OtherCardEntity otherCardEntity;
    //WX
    KaQuanXiangQingEntity.DataBean.ListBean kaquanxiangqingEntity;
    //koubei
    KouBeiTicktEntity.DataBean kouBeiTicktEntity;
    //卡券码
    String erWeiMaResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_kaquandetail);
        ButterKnife.bind(this);
        initToolbarWithBack(0);
        initUI();
        initData();
    }

    public void initToolbarWithBack(int backResId) {
        if("koubei".equals(getIntent().getStringExtra("tag"))) {
            toolbarTitle.setText("口碑券详情");
        }
        if(mToolbar != null) {
            int rid = backResId == 0 ? defalutBackIconResID : backResId;
            mToolbar.setNavigationIcon(rid);
            mToolbar.setNavigationOnClickListener(v -> finish());
        }
    }

    private void initUI() {
        type = getIntent().getStringExtra("type");
        erWeiMaResult = getIntent().getStringExtra("erWeiMaResult");
        //微信
        if(CardType.WX.name().equals(type)) {
            llKouBeiAbout.setVisibility(View.GONE);
            llWx.setVisibility(View.VISIBLE);
            llJiuDian.setVisibility(View.GONE);
            llLiPin.setVisibility(View.GONE);
            llKouBei.setVisibility(View.GONE);
            llTuanGou.setVisibility(View.GONE);
            kaquanxiangqingEntity =
                    (KaQuanXiangQingEntity.DataBean.ListBean) getIntent().getSerializableExtra("Data");
            tvCardNum.setText(erWeiMaResult);
            tvWxCardName.setText(kaquanxiangqingEntity.getName());
            tvWxCardTime.setText(kaquanxiangqingEntity.getBegintime().split(" ")[0] +
                    "~" + kaquanxiangqingEntity.getEndtime().split(" ")[0]);
            if(kaquanxiangqingEntity.getType().equals("0")) {
                tvWxType.setText("优惠券");
                llWXZheKou.setVisibility(View.GONE);
                llWXDaiJin.setVisibility(View.GONE);
                tvWxType.setBackgroundColor(getResources().getColor(R.color.y_color));
            }
            if(kaquanxiangqingEntity.getType().equals("1")) {
                tvWxType.setText("抵用券");
                llWXZheKou.setVisibility(View.GONE);
                llWXDaiJin.setVisibility(View.GONE);
                tvWxType.setBackgroundColor(getResources().getColor(R.color.d_color));
            }
            if(kaquanxiangqingEntity.getType().equals("2")) {
                tvWxType.setText("折扣券");
                llWXZheKou.setVisibility(View.VISIBLE);
                llWXDaiJin.setVisibility(View.GONE);
                SpannableString spannableString =
                        new SpannableString(kaquanxiangqingEntity.getDescription());
                spannableString.setSpan(new AbsoluteSizeSpan(100), 0, 4,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvZheKouOrder.setText(spannableString);
                tvWxType.setBackgroundColor(getResources().getColor(R.color.z_color));
            }
            if(kaquanxiangqingEntity.getType().equals("3")) {
                tvWxType.setText("代金券");
                llWXZheKou.setVisibility(View.GONE);
                llWXDaiJin.setVisibility(View.VISIBLE);
                SpannableString spannableString =
                        new SpannableString(kaquanxiangqingEntity.getAmount());
                spannableString.setSpan(new AbsoluteSizeSpan(100), 0,
                        spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvDaiJinOrder.setText(spannableString + "元");
                tvDaiJinOrderDetail.setText("满" + kaquanxiangqingEntity.getMinmoney() + "元可用");
                tvWxType.setBackgroundColor(getResources().getColor(R.color.d_color));
            }
            if(kaquanxiangqingEntity.getType().equals("4")) {
                tvWxType.setText("礼品券");
                llWXZheKou.setVisibility(View.GONE);
                llWXDaiJin.setVisibility(View.GONE);
                tvWxType.setBackgroundColor(getResources().getColor(R.color.l_color));
            }
            if(kaquanxiangqingEntity.getType().equals("5")) {
                tvWxType.setText("团购券");
                llWXZheKou.setVisibility(View.GONE);
                llWXDaiJin.setVisibility(View.GONE);
                tvWxType.setBackgroundColor(getResources().getColor(R.color.t_color));
            }
            //--------暂定为6-----------------------------等后端给
            if(kaquanxiangqingEntity.getType().equals("6")) {
                tvWxType.setText("次卡");
                llWXZheKou.setVisibility(View.GONE);
                llWXDaiJin.setVisibility(View.GONE);
                mRlTimeCard.setVisibility(View.VISIBLE);
                mTvTimeCard.setText(kaquanxiangqingEntity.getSurpluscount()+"次");
                tvWxType.setBackgroundColor(Color.parseColor("#65B45B"));

            }

            tvAboutContent.setText(kaquanxiangqingEntity.getUsershuoming());

        }
        //酒店
        if(CardType.JIUDIAN.name().equals(type)) {
            llKouBeiAbout.setVisibility(View.GONE);
            llWx.setVisibility(View.GONE);
            llJiuDian.setVisibility(View.VISIBLE);
            llLiPin.setVisibility(View.GONE);
            llKouBei.setVisibility(View.GONE);
            llTuanGou.setVisibility(View.GONE);
            otherCardEntity = (OtherCardEntity) getIntent().getSerializableExtra("Data");
            tvCardNum.setText(erWeiMaResult);
            rlAbout.setVisibility(View.GONE);
//            tvAboutContent.setText(otherCardEntity.getData().getExplain());
            tvJiuDianName.setText(otherCardEntity.getData().getTitle());
            tvJiuDianMoney.setText(otherCardEntity.getData().getOrderMoney() + "元");
            tvJiudianOrderTime.setText(otherCardEntity.getData().getCreatetime());
            if("0".equals(otherCardEntity.getData().getOrderState())) {
                tvJiuDianState.setText("未付款");
            } else if("1".equals(otherCardEntity.getData().getOrderState())) {
                tvJiuDianState.setText("已支付");
            } else if("2".equals(otherCardEntity.getData().getOrderState())) {
                tvJiuDianState.setText("已到店");
            } else if("3".equals(otherCardEntity.getData().getOrderState())) {
                tvJiuDianState.setText("已离店");
            } else {
                tvJiuDianState.setText("已取消");
            }
            //少一个联系电话的字段
            tvJiudianContract.setText(otherCardEntity.getData().getPhone());
        }
        //口碑
        if(CardType.KOUBEI.name().equals(type)) {
            llWx.setVisibility(View.GONE);
            llJiuDian.setVisibility(View.GONE);
            llLiPin.setVisibility(View.GONE);
            llKouBei.setVisibility(View.VISIBLE);
            llTuanGou.setVisibility(View.GONE);
            llKouBeiAbout.setVisibility(View.VISIBLE);
            rlAbout.setVisibility(View.GONE);
            kouBeiTicktEntity =
                    (KouBeiTicktEntity.DataBean) getIntent().getSerializableExtra("Data");
            tvCardNum.setText(erWeiMaResult);
            tvKouBeiTime.setText(kouBeiTicktEntity.getEffectDate().split(" ")[0] + "~" + kouBeiTicktEntity.getExpireDate().split(" ")[0]);
            tvKouBeiOrder.setText(kouBeiTicktEntity.getTicketCode());
            tvKouBeiType.setText("口碑券");
            tvKouBeiName.setText(kouBeiTicktEntity.getItemName());
            tvKouBeiRealMoney.setText(kouBeiTicktEntity.getOriginalPrice() + "元");
            tvKouBeiNowMoney.setText(kouBeiTicktEntity.getCurrentPrice() + "元");
            tvKouBeiShiShou.setText(kouBeiTicktEntity.getCurrentPrice() + "元");

        }
        //礼品
        if(CardType.LIPIN.name().equals(type)) {
            llKouBeiAbout.setVisibility(View.GONE);
            llWx.setVisibility(View.GONE);
            llJiuDian.setVisibility(View.GONE);
            llLiPin.setVisibility(View.VISIBLE);
            llKouBei.setVisibility(View.GONE);
            llTuanGou.setVisibility(View.GONE);
            otherCardEntity = (OtherCardEntity) getIntent().getSerializableExtra("Data");
            tvCardNum.setText(erWeiMaResult);
            tvOrderTypeLiPin.setText("礼品券");
            tvLiPinName.setText(otherCardEntity.getData().getTitle());
            tvLiPinTime.setText(otherCardEntity.getData().getBegtime().split(" ")[0] +
                    "~" + otherCardEntity.getData().getEndtime().split(" ")[0]);
            tvLiPinJifen.setText(otherCardEntity.getData().getNeedIntegral() + "积分");
            tvLiPinUserName.setText(otherCardEntity.getData().getUserName());
            rlAbout.setVisibility(View.GONE);
//            tvAboutContent.setText(otherCardEntity.getData().getExplain());
        }
        //门票和团购
        if(CardType.MENPIAOANDTUANGOU.name().equals(type)) {
            llKouBeiAbout.setVisibility(View.GONE);
            llWx.setVisibility(View.GONE);
            llJiuDian.setVisibility(View.GONE);
            llLiPin.setVisibility(View.GONE);
            llKouBei.setVisibility(View.GONE);
            llTuanGou.setVisibility(View.VISIBLE);
            otherCardEntity = (OtherCardEntity) getIntent().getSerializableExtra("Data");
            tvCardNum.setText(erWeiMaResult);
            tvTuanGouMoney.setText(otherCardEntity.getData().getOrderMoney() + "元");
            tvTuanGouName.setText(otherCardEntity.getData().getTitle());
            tvTuanGouOderTime.setText(otherCardEntity.getData().getCreatetime());
            rlAbout.setVisibility(View.VISIBLE);
            tvAboutContent.setText(otherCardEntity.getData().getExplain());
            if(erWeiMaResult.startsWith("34")) {
                tvTuanGouType.setText("门票");
                tvTuanGouType.setBackgroundColor(getResources().getColor(R.color.d_color));
            }
            //差一个团购券
            if(erWeiMaResult.startsWith("25")) {
                tvTuanGouType.setText("团购券");
                tvTuanGouType.setBackgroundColor(getResources().getColor(R.color.t_color));
            }

            if("1".equals(otherCardEntity.getData().getType())) {
                if("0".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("待支付");
                } else if("1".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已支付");
                } else if("2".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已支付");
                } else if("3".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已核销");
                } else {
                    tvTuanGouState.setText("已取消");
                }
            } else {
                if("0".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("未支付");
                } else if("1".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已支付");
                } else if("2".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已发货");
                } else if("3".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已完成");
                } else if("4".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("已失效");
                } else if("5".equals(otherCardEntity.getData().getOrderState())) {
                    tvTuanGouState.setText("申请退款");
                } else {
                    tvTuanGouState.setText("已退款");
                }
            }
        }
    }

    private void initData() {

    }

    @OnClick({R.id.btn_hexiao})
    public void onViewClicked(View view) {
        switch(view.getId()) {
            case R.id.btn_hexiao:
                if(CardType.WX.name().equals(type)) {
                    if(kaquanxiangqingEntity.getType().equals("2") || kaquanxiangqingEntity.getType().equals("3")) {
                        //2是折扣券 3 是代金券
                        Intent intent = new Intent(this, KaQuanInputMoneyActivity.class);
                        intent.putExtra("erWeiMaResult", erWeiMaResult);
                        intent.putExtra("Data", kaquanxiangqingEntity);
                        startActivity(intent);
                        finish();
                    } else {
                        responseNetForHexiao(erWeiMaResult);
                    }
                } else if(CardType.KOUBEI.name().equals(type)) {
                    responseNetForHexiao(erWeiMaResult);
                } else {
                    responseNetForHexiao(erWeiMaResult);
                }
                break;
        }
    }

    //请求网络为了 核销该张优惠券
    private void responseNetForHexiao(String scanTicketNo) {
        if(getIntent().getStringExtra("tag").equals("xianxia")) {
            HashMap<String, String> addParams = new HashMap<>();
            addParams.put("scanTicketNo", scanTicketNo);//86008032893055438199  test
//            addParams.put("payMoney", minmoney);
            APIRetrofit.getAPIService().getKaQuanHeXiao(RXRequest.getXingPosParams(addParams, this))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BlockingBaseObserver<KaQuanHeXiaoEntity>() {
                        @Override
                        public void onNext(KaQuanHeXiaoEntity kaQuanHeXiaoEntity) {
                            int success = kaQuanHeXiaoEntity.getSuccess();
                            if(success == 1) {
                                Intent intent = new Intent(KaQuanDetailNewActivity.this
                                        , CardUseSuccessNewActivity.class);
                                if(erWeiMaResult.startsWith("21")) {
                                    intent.putExtra("type", CardType.JIUDIAN.name());
                                    intent.putExtra("Data", otherCardEntity);
                                } else if(erWeiMaResult.startsWith("34")) {
                                    intent.putExtra("type",
                                            CardType.MENPIAOANDTUANGOU.name());
                                    intent.putExtra("Data", otherCardEntity);
                                } else if(erWeiMaResult.startsWith("25")) {
                                    intent.putExtra("type",
                                            CardType.MENPIAOANDTUANGOU.name());
                                    intent.putExtra("Data", otherCardEntity);
                                } else if(erWeiMaResult.startsWith("8")) {
                                    intent.putExtra("type", CardType.LIPIN.name());
                                    intent.putExtra("Data", otherCardEntity);
                                } else {
                                    intent.putExtra("type", CardType.WX.name());
                                    intent.putExtra("Data", kaquanxiangqingEntity);
                                }
                                intent.putExtra("kaquan", kaQuanHeXiaoEntity);
                                intent.putExtra("tag", "xianxia");
                                intent.putExtra("erWeiMaResult", erWeiMaResult);
                                startActivity(intent);
                                finish();
                                //
//   intent.putExtra("memberNo", scanTicketNo);
//                                intent.putExtra("member", kaQuanHeXiaoEntity.getData
//                                ().getMembername());
//                                intent.putExtra("uTime", kaQuanHeXiaoEntity.getData()
//                                .getTousedate());
//                                intent.putExtra("title", kaQuanHeXiaoEntity.getData()
//                                .getTitle());
//                                intent.putExtra("touse", kaQuanHeXiaoEntity.getData()
//                                .getTouse());
//                                String needpay = kaQuanHeXiaoEntity.getData()
//                                .getNeedpay();
//                                double Dneedpay = Double.parseDouble(needpay);
//                                if (Dneedpay > 0) {
//                                    //之前版本!!
//                                    intent.putExtra("paymoney", needpay);
//                                }
//                                startActivity(intent);
//                                finish();
                            } else {
                                String errMsg = kaQuanHeXiaoEntity.getErrMsg();
                                ToastUtils.showToast(KaQuanDetailNewActivity.this,
                                        errMsg + "");
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }
                    });
        }
        if(getIntent().getStringExtra("tag").equals("koubei")) {
            HashMap<String, String> addParams = new HashMap<>();
            addParams.put("TicketID", scanTicketNo);//86008032893055438199  test
            APIRetrofit.getAPIService().useKoubeiQuan(RXRequest.getXingPosParams(addParams, this))
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BlockingBaseObserver<KouBeiUseEntity>() {
                        @Override
                        public void onNext(KouBeiUseEntity kouBeiUseEntity) {
                            int success = kouBeiUseEntity.getSuccess();
                            if(success == 1) {
                                Intent intent = new Intent(KaQuanDetailNewActivity.this
                                        , CardUseSuccessNewActivity.class);
                                intent.putExtra("type", CardType.KOUBEI.name());
                                intent.putExtra("Data", kouBeiUseEntity);
                                intent.putExtra("koubei", kouBeiTicktEntity);
                                intent.putExtra("erWeiMaResult", erWeiMaResult);
                                startActivity(intent);
                                finish();
                            } else {
                                String errMsg = kouBeiUseEntity.getErrMsg();
                                ToastUtils.showToast(KaQuanDetailNewActivity.this,
                                        errMsg + "");
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }
                    });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}

